<template>
	<view class="striveOrders-wrap" v-if="questionOrder">
		<view class="striveOrders-price-mod">
			<u-title value="付款详情"></u-title>
			<view class="striveOrders-price-num">
				<view class="word3">{{ orderTypeList[commodityOrder.orderType].name }}-{{ inquiryTypeList[commodityOrder.inquiryType].name }}</view>
				<view class="txt1">¥&nbsp;{{ commodityOrder.totalAmount }}</view>
			</view>

			<view class="striveOrders-price-num" v-if="questionOrder.subTime">
				<view class="word3">预约时间</view>
				<view class="txt1">{{ questionOrder.subTime }}</view>
			</view>
		</view>

		<view class="striveOrders-drug" v-if="commodityOrder.orderType == 2 && questionOrder.drugQuestionReq">
			<u-title value="已选药品"></u-title>
			<view class="striveOrders-drug-list"><u-show-goods v-for="(item, index) in questionOrder.drugQuestionReq.commodityList" :key="index" :data="item"></u-show-goods></view>
		</view>

		<view class="striveOrders-patient">
			<u-title value="问诊信息"></u-title>
			<view class="striveOrders-patient-box">
				<view class="striveOrders-patient-outer">
					<text lines="1" class="word9">患者信息：</text>
					<text lines="1" class="word10">{{ patient.patientName }}&nbsp;&nbsp;{{ rangeSex[patient.sex].text }}&nbsp;&nbsp;{{ patient.phone }}</text>
				</view>
				<view class="striveOrders-patient-outer" v-if="commodityOrder.orderType == 2">
					<text lines="1" class="word9">所患疾病：</text>
					<text lines="1" class="word10">{{ questionOrder.content }}</text>
				</view>

				<view class="striveOrders-patient-outer" v-if="commodityOrder.orderType == 2">
					<text lines="1" class="word9">肝功能：</text>
					<text lines="1" class="word10">{{ functionList[questionOrder.drugQuestionReq.liver].name }}</text>
				</view>

				<view class="striveOrders-patient-outer" v-if="commodityOrder.orderType == 2">
					<text lines="1" class="word9">肾功能：</text>
					<text lines="1" class="word10">{{ functionList[questionOrder.drugQuestionReq.kidney].name }}</text>
				</view>

				<view class="striveOrders-patient-outer" v-if="commodityOrder.orderType == 2 && questionOrder.drugQuestionReq.allergy == 1">
					<text lines="1" class="word9">过敏史：</text>
					<text lines="1" class="word10">{{ questionOrder.drugQuestionReq.allergyText }}</text>
				</view>

				<view class="striveOrders-patient-outer" v-if="commodityOrder.orderType == 2">
					<text lines="1" class="word9">生育状态及计划：</text>
					<text lines="1" class="word10">{{ birthStatusList[questionOrder.drugQuestionReq.birthStatus].name }}</text>
				</view>

				<view class="striveOrders-patient-outer" v-if="commodityOrder.orderType != 2">
					<text lines="1" class="word9">患病时间：</text>
					<text lines="1" class="word10">{{ durationList[questionOrder.duration].name }}</text>
				</view>
				<view class="striveOrders-patient-outer" v-if="commodityOrder.orderType != 2">
					<text lines="1" class="word9">就诊情况：</text>
					<text lines="1" class="word10">{{ isLookHospList[questionOrder.isLookHosp].name }}</text>
				</view>
				<view class="striveOrders-patient-outer" v-if="commodityOrder.orderType != 2">
					<text lines="1" class="word9">病情描述：</text>
					<text lines="1" class="word10">{{ questionOrder.content }}</text>
				</view>
			</view>
		</view>

		<view class="striveOrders-report" v-if="reportList.length > 0">
			<u-title value="处方证明"></u-title>
			<view class="striveOrders-report-list"><image :src="item" class="striveOrders-report-item" v-for="(item, index) in reportList" :key="index" @click="previewImage(reportList,index)"></image></view>
		</view>

		<view class="striveOrders-patient">
			<u-title value="订单信息"></u-title>
			<view class="striveOrders-patient-box">
				<view class="striveOrders-patient-outer">
					<text lines="1" class="word10">订单编号：{{ commodityOrder.outTradeNo }}</text>
					<view class="word-btn" @click="setClipboardData(commodityOrder.outTradeNo)">复制</view>
				</view>
				<view class="striveOrders-patient-outer">
					<text lines="1" class="word10">下单时间：{{ commodityOrder.orderTime }}</text>
				</view>
				<view class="striveOrders-patient-outer">
					<text lines="1" class="word10">优惠金额：{{ commodityOrder.couponMoney }} 元</text>
				</view>
				<view class="striveOrders-patient-outer">
					<text lines="1" class="word10">支付金额：{{ commodityOrder.orderAmount }} 元</text>
				</view>
			</view>
		</view>

		<view class="striveOrders-bottom iphonex-footer">
			<view class="striveOrders-bottom-box">
				<view class="striveOrders-bottom-btn" @click="previousOrder">上一条</view>
				<view class="striveOrders-bottom-core" @click="robOrder">抢单</view>
				<view class="striveOrders-bottom-btn" @click="nextOrder">下一条</view>
			</view>
		</view>
	</view>
</template>

<script>
import uTitle from '@/components/u-title/u-title.vue';
import uBottomButton from '@/components/u-bottom-button/u-bottom-button.vue';
import uDoctorItem from '@/components/u-doctor-item/u-doctor-item.vue';
import uShowGoods from '@/components/u-show-goods/u-show-goods.vue';
export default {
	components: {
		uBottomButton,
		uTitle,
		uDoctorItem,
		uShowGoods
	},
	data() {
		return {
			doctorDetail: {},
			patient: {},
			commodityOrder: null, //订单信息
			questionOrder: null, //问诊详情
			reportList: [],
			id: null, //问诊id
			doctorId: null, //订单id
			orderCentterList: [],
			orderIndex: 0, //当前订单下标
			orderId: null, //当前订单id
			orderCentterItem: null
		};
	},
	computed: {},
	onLoad(option) {
		this.orderIndex = option.index ? option.index : 0;
		this.orderCenter();
	},
	onShow() {},
	methods: {
		//订单详情
		questionOrderDetail(id) {
			this.$api
				.questionOrderDetail({
					params: {
						id: this.id
					}
				})
				.then(res => {
					if (res.code == 200) {
						if (res.result) {
							this.commodityOrder = res.result.commodityOrder;
							this.patient = res.result.patient;
							this.questionOrder = res.result.questionOrder;
							this.doctorId = res.result.questionOrder.doctorId;
							if (this.doctorId) {
								this.queryDoctorDetail();
							}

							this.reportList = res.result.questionOrder.reportList;
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},

		//医生详情
		queryDoctorDetail() {
			this.$api
				.queryDoctorDetail({
					params: {
						id: this.doctorId
					}
				})
				.then(res => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
					if (res.code == 200) {
						this.doctorDetail = res.result;
					}
				});
		},

		//医生抢单中心
		orderCenter() {
			this.$api
				.orderCenter({
					params: {
						pageNo: 1,
						pageSize: 999
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.orderCentterList = res.result.records;
						if (this.orderCentterList.length > 0) {
							this.orderCentterItem = this.orderCentterList[this.orderIndex];
							this.switchInquiryOrder(this.orderCentterItem.id);
						}
					}
				});
		},

		//切换问诊订单
		switchInquiryOrder(id) {
			this.id = id;
			this.questionOrderDetail();
		},

		//下一条
		nextOrder() {
			if (this.orderIndex < this.orderCentterList.length-1) {
				this.orderIndex++;
			} else {
				this.orderIndex = 0;
			}
			this.orderCentterItem = this.orderCentterList[this.orderIndex];
			this.switchInquiryOrder(this.orderCentterItem.id);
		},

		//上一条
		previousOrder() {
			if (this.orderIndex > 0) {
				this.orderIndex--;
			} else {
				this.orderIndex = this.orderCentterList.length - 1;
			}
			this.orderCentterItem = this.orderCentterList[this.orderIndex];
			this.switchInquiryOrder(this.orderCentterItem.id);
		},

		//医生抢单
		robOrder() {
			this.$api
				.robOrder({
					params: {
						id: this.questionOrder.id
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.gotoChatRoom(this.commodityOrder.userId);
						uni.showToast({
							title: '抢单成功',
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},
		
		
		//首次进入聊天室
		gotoChatRoom(accid) {
			getApp().globalData.nim.addFriend({
				account: accid,
				ps: '',
				done: (err, obj) => {
					if (err) {
						console.log(err);
						uni.showToast({
							title: "发起聊天失败",
							icon: 'none'
						});
						
						return;
					} else {
						/*
						this.$store.dispatch('im/CurrentChatTo_Change', {
							session: 'p2p-' + accid,
							account: accid
						});
						uni.reLaunch({
							url: `/pages-im/chatRoom/chatRoom?accid=${accid}&role=1&questionOrderId=${this.questionOrder.id}`
						})
						*/
						this.$store.dispatch('txyIm/CurrentChatTo_Change', {
							session: 'C2C' + accid,
							account: accid
						});
						uni.reLaunch({
							url: `/pages-txy-im/chatRoom/chatRoom?accid=${accid}&role=1&questionOrderId=${this.questionOrder.id}`
						})
					}
		
				}
			});
		}
	}
};
</script>

<style lang="less">
@import './striveOrders.less';
</style>
